using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SPGael.CustomFields
{
    class CustomRegularExpressionValidatorFieldControl : BaseFieldControl
    {
        private const string RENDERING_TEMPLATE = "CustomRegularExpressionValidatorControl";

        protected TextBox txtValueToValidate;

        protected override string DefaultTemplateName
        {
            get { return RENDERING_TEMPLATE; }
        }

        protected override void CreateChildControls()
        {
            if (this.Field == null
                || this.ControlMode == SPControlMode.Display
                || this.ControlMode == SPControlMode.Invalid)
                return;

            base.CreateChildControls();

            // get and check the control in the template
            this.txtValueToValidate = TemplateContainer.FindControl("txtValueToValidate") as TextBox;
            if (this.txtValueToValidate == null)
                throw new ArgumentException("Value to validate text box not found. Please check if your control template is valid.");
        }

        public override object Value
        {
            get
            {
                EnsureChildControls();
                return this.txtValueToValidate.Text;
            }
            set
            {
                EnsureChildControls();
                this.txtValueToValidate.Text = (String)this.ItemFieldValue;
            }
        }
    }
}
